package sketchpad.gui.component;

import com.jfoenix.controls.JFXChipView;
import com.jfoenix.controls.JFXDefaultChip;
import com.jfoenix.controls.JFXListCell;
import com.jfoenix.effects.JFXDepthManager;
import javafx.geometry.NodeOrientation;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.util.StringConverter;
import sketchpad.constant.*;
import sketchpad.gui.component.utils.ItemIco;
import sketchpad.gui.drawinggraph.Random;
import sketchpad.gui.control.DrawControl;
import sketchpad.gui.drawinggraph.DrawItem;
import sketchpad.gui.drawinggraph.node.Node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItemChipView {

    private JFXChipView<Label> chipView;
    private DrawControl control;

    public ItemChipView(DrawControl drawControl){
        control = drawControl;
        chipView=new JFXChipView<>();
    }

    public JFXChipView<Label> constructItemChipView() throws Exception {

        chipView.setChipFactory((emailJFXChipView, email) -> new JFXDefaultChip<Label>(emailJFXChipView, email){
            {
                if ( getItem().getGraphic() != null) {
                    getItem().getGraphic().getStyleClass().add("chip-icon");
                    root.getChildren().add(0, getItem().getGraphic());
                }
            }
        });


        HashMap<String, Label> suggestions = new HashMap<>();
        ItemIco itemIco = new ItemIco();
        suggestions.put(CHNName.TRIANGLE, new Label(CHNName.TRIANGLE, itemIco.getTriangle()));
        suggestions.put(CHNName.CIRCLE, new Label(CHNName.CIRCLE, itemIco.getCircle()));
        suggestions.put(CHNName.RECTANGLE, new Label(CHNName.RECTANGLE, itemIco.getRectangle()));
        suggestions.put(CHNName.RANDOM, new Label(CHNName.RANDOM, itemIco.getRandom()));
        suggestions.put(CHNName.SELECT, new Label(CHNName.SELECT, itemIco.getSelect()));
        suggestions.put(CHNName.GROUP, new Label(CHNName.GROUP, itemIco.getGroup()));

        chipView.setConverter(new StringConverter<Label>() {
            @Override
            public String toString(Label object) {
                return object.getText();
            }

            @Override
            public Label fromString(String string) {
                Label found = suggestions.get(string);
                return found == null ? new Label(string, null) : found;
            }
        });
        chipView.getSuggestions().addAll(suggestions.values());
        chipView.setSuggestionsCellFactory(param -> new JFXListCell<>());

        JFXDepthManager.setDepth(chipView, 2);

        chipView.setLayoutX(0);
        chipView.setLayoutY(10);
        chipView.setMinHeight(120);
        chipView.setMaxHeight(120);
        chipView.setMinWidth(Config.WINDOW_LENGTH);
        chipView.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
        return chipView;
    }

    public void addItem() throws Exception{
        chipView.getChips().clear();
        Map<Integer, DrawItem> drawItemMap = control.getDrawItemMap();
        List<Label> list = new ArrayList<>();
        ItemIco itemIco = new ItemIco();
        if (!control.getSelectedItemList().isEmpty())list.add(new Label(CHNName.SELECT, itemIco.getSelect()));
        for (DrawItem item : drawItemMap.values()){
            if (item.getId()==Config.SELECT_ID
            || control.getSelectedItemList().contains(item.getId())
            || (item.getGeometryClass() != Geometry.RANDOM
                    && ((Math.abs(item.getLeftUpNode().x-item.getRightDownNode().x)< Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS)
                    || Math.abs(item.getLeftUpNode().y-item.getRightDownNode().y)<Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS))) continue;
            itemIco = new ItemIco();
            Label shape;
            switch (item.getGeometryClass()){
                case TRIANGLE:
                    shape = new Label(CHNName.TRIANGLE,itemIco.getTriangle());
                    break;
                case RANDOM:
                    List<Node> nodes = ((Random)item).getNodeList();
                    double minX = Double.MAX_VALUE;
                    double maxX = Double.MIN_VALUE;
                    double minY = Double.MAX_VALUE;
                    double maxY = Double.MIN_VALUE;
                    for (int i = 0; i < nodes.size(); i++){
                        minX = Math.min(nodes.get(i).x,minX);
                        maxX = Math.max(nodes.get(i).x,maxX);
                        minY = Math.min(nodes.get(i).y,minY);
                        maxY = Math.max(nodes.get(i).y,maxY);
                    }
                    if ((maxX-minX) < Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS && (maxY-minY) < Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS)continue;
                    shape = new Label(CHNName.RANDOM,itemIco.getRandom());
                    break;
                case RECTANGLE:
                    shape = new Label(CHNName.RECTANGLE,itemIco.getRectangle());
                    break;
                case CIRCLE:
                    shape = new Label(CHNName.CIRCLE,itemIco.getCircle());
                    break;
                default:
                    shape = new Label("",null);
            }
            list.add(shape);
        }
        chipView.getChips().addAll(list);
    }
}
